﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;

namespace IronOne.SalesOne.DataObjects
{

    public class LinqEmails
    {

        public int InsertEmails(Emails e)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {

                    EmailsEntity eentity = Mapper.ToEmailEntity(e);
                    db.EmailsEntities.Attach(eentity,true);
                    db.SubmitChanges();

                    e.JobId = eentity.JobId;
                    return 1;

                }
                catch (ChangeConflictException)
                {
                    return 0;
                    //throw new Exception("AAAA.");
                    
                }

                catch (Exception)
                {
                    return 0;
                    //throw new Exception("BBBB");
                    
                }
            }
           
        }



        public List<string> GetAvailableMailJobs()
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return (from email in db.EmailsEntities
                        select email.JobId).ToList();
               
            }
        }

        public Emails getJobInfo(string jobId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                EmailsEntity query = db.EmailsEntities.SingleOrDefault(p => p.JobId == jobId);
                return Mapper.ToEmailObject(query);
            }
        }
    }
}
